Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        wxCode: '',
        isAgree: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let userInfo = wx.getStorageSync('userInfo');
        if(userInfo){
            wx.$utils.goBack();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getCode();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    //查看协议
    goAgreement: function(e){
        let type = e.currentTarget.dataset.type;
        wx.navigateTo({
            url: `/pages/other/agreement/index?type=${type}`,
        })
    },

    //设置是否同意
    setAgree: function(){
        let {isAgree} = this.data;
        this.setData({
            isAgree: !isAgree
        })
    },

    //获取code
    getCode: function(){
        let _this = this;
        wx.login({
            success: (res) => {
                if(res.errMsg == 'login:ok'){
                    console.log(res.code,'code')
                    _this.setData({
                        wxCode: res.code
                    })
                }else{
                    wx.showToast({
                        title: '获取code失败,稍后再试！',
                        icon: 'none'
                    })
                }
            },
        })
    },
    //提示阅读
    tipAgree: function(){
        wx.showToast({
            title: '请阅读并同意服务隐私协议',
            icon: 'none'
        })
    },
    //关闭弹窗
    closePop: function() {
        this.setData({
            is_phone_pop: false
        })  
    },
    //登录
    getUserProfile: function() {
        let {wxCode} = this.data;
        wx.$http({
            load: true,
            url: '/business/login',
            data: {
                wx_code: wxCode
            },
        }).then((res) => {
            let data = res.data;
            if(data.code == 1){
                wx.setStorageSync('userInfo',data);
                wx.setStorageSync('token',data.token);
                wx.$utils.goBack();
            }else{
                this.setData({
                    is_phone_pop: true
                })
            }
        }).catch(err=>{
            wx.showToast({
                title: '登录失败，请稍后再试',
                icon: 'none'
            })
        }).finally(()=>{
            this.getCode();
        })
    },
    //手机号登录
    setPhoneLogin: function(e){
        let {wxCode} = this.data;
        let errno = e.detail.errno;
        let phoneCode = e.detail.code;
        let encryptedData = e.detail.encryptedData;
        let iv = e.detail.iv;
        if(errno || !phoneCode){
            wx.showToast({
                title: '登录失败，请稍后再试',
                icon: 'none'
            })
            return;
        }
        if(!wxCode){
            this.getCode();
        }
        wx.showLoading();
        wx.$http({
            load: true,
            url: '/business/register',
            data: {
                wx_code: wxCode,
                phone_code: phoneCode,
                encryptedData: encryptedData,
                iv: iv
            },
        }).then((res) => {
            let data = res.data;
            wx.setStorageSync('userInfo',data);
            wx.setStorageSync('token',data.token);
            wx.$utils.goBack();
        }).catch(err=>{
            wx.showToast({
                title: '登录失败，请稍后再试',
                icon: 'none'
            })
        }).finally(()=>{
            this.getCode();
        })
    }
})



